package com.sc.fs3.controller;

import com.sc.fs3.common.Result;
import com.sc.fs3.pojo.FileEntity;
import com.sc.fs3.repository.DocRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 前后端一体版
 * 本项目
 */
@Controller
@Slf4j
public class DocController {

    @Resource
    private DocRepository docRepository;   //es api


    /**
     * 通过关键词搜索
     * @param request
     * @return
     */
    @ApiOperation("搜索接口")
    @RequestMapping("/search")
    public ModelAndView fullTextSearch(HttpServletRequest request){
        String keyWord = request.getParameter("keyWord");
        List<FileEntity> list = new ArrayList<>();
        //查询关键字匹配
        list =  docRepository.queryFileEntitiesByTitleMatchesOrContentMatches(keyWord,keyWord);
        //查询最近创建的top20
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("top20",list);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("ResultData");
        mv.addObject("map",map);
        return mv;
    }


    /**
     * 新增文档（页面方式）
     * @param request
     * @return
     */
    @ApiOperation("索引文档")
    @RequestMapping("/insertnew")
    @ResponseBody
    public Result insertDocument(HttpServletRequest request){
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        FileEntity fileEntity = new FileEntity();
        LocalDateTime date = LocalDateTime.now();
        String id = date.toString() + (int)(Math.random() * 10);
        fileEntity.setId(id)  //随机生成id
                .setTitle(title)    //文章标题
                .setContent(content)    //文章内容
                .setUploadtime(date)    //上传时间
                .setSize(1000L);        //文件大小

        log.info("--params: " + fileEntity.toString());
        //全文索引中(存储)
        FileEntity save = docRepository.save(fileEntity);
        log.info("Indexing: " + save);     //如果成功，则返回该对象
        if (save != null ) {
            log.info("indexed success !");
            return Result.ok();
        } else{
            log.info("indexed failure !");
            return Result.error();
        }
    }


    /**
     * 编辑文档
     * @param request
     * @return
     */
    @RequestMapping("/edit")
    public ModelAndView editText(HttpServletRequest request){
        String id = request.getParameter("id");
        Optional<FileEntity> optional = docRepository.findById(id);
        FileEntity fileEntity = optional.get();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("EditData");
        mv.addObject("fileEntity",fileEntity);
        return mv;
    }


    /**
     * 更新文档
     * @param request
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result updateDocument(HttpServletRequest request){
        String id = request.getParameter("id");
        log.info("唯一 --："+ id);
        String content = request.getParameter("content");
        log.info("update -- :" + content);
        Optional<FileEntity> optional = docRepository.findById(id);
        FileEntity fileEntity = optional.get();
        //更改后的文章内容
        fileEntity.setContent(content);
        FileEntity save = docRepository.save(fileEntity);
        if (save != null){
            log.info("update success !");
            return Result.ok();
        }else{
            log.info("update failure");
            return Result.error();
        }
    }

}
